<template>
  <div>
    <el-tree :data="menus" :default-expanded-keys="expandedkeys" node-key="catId" show-checkbox :props="defaultProps" :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button v-if="node.level <= 2" type="text" size="mini" @click="() => append(data)">
            添加
          </el-button>
          <el-button v-if="node.level <= 2" type="text" size="mini" @click="() => edit(data)">
            编辑
          </el-button>
          <el-button v-if="node.childNodes.length == 0" type="text" size="mini" @click="() => remove(node, data)">
            删除
          </el-button>
        </span>
      </span>
    </el-tree>

    <!-- 对话框 -->
    <el-dialog :title=title :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false">
      <el-form :model="category">
        <el-form-item label="分类名称">
          <el-input v-model="category.name"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: "",
      dialogType: "",
      category: { name: "", parentCid: 0, catLevel: 0, showStatus: 1, sort: 0 },//绑定添加或修改的对话框
      dialogVisible: false,
      menus: [],
      expandedkeys: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  created() {
    this.getMenus();
  },
  methods: {
    submitData() {
      if (this.dialogType == "add") {
        this.addCategory();
      }
      if (this.dialogType == "edit") {
        this.editCategory();
      }
      console.info("提交的数据是", this.category);
    },

    editCategory() {
      var { catId, name } = this.category;
      this.$http({
        url: this.$http.adornUrl('/product/category/update'),
        method: 'post',
        data: this.$http.adornData({ catId, name }, false)
      }).then(({ data }) => {
        this.$message({
          message: "菜单修改成功",
          type: "success",
        })
        //关闭对话框
        this.dialogVisible = false;
        //刷新出新的菜单
        this.getMenus();
        //设置默认展开的菜单
      });
    },

    addCategory() {
      console.log("addCategory提交的三级分类数据", this.category),
        this.$http({
          url: this.$http.adornUrl('/product/category/save'),
          method: 'post',
          data: this.$http.adornData(this.category, false)
        }).then(({ data }) => {
          this.$message({
            message: "菜单保存成功",
            type: "success"
          })
          //关闭对话框
          this.dialogVisible = false;
          this.getMenus();
          console.log("应展开", this.category.parentCid)
          //this.expandedKeys = [this.category.parentCid];
          var a = this.category.parentCid
          this.expandedkeys = [a]
        });
    },

    edit(data) {
      console.log("要修改的数据是", data),
        this.title = "编辑分类",
        this.dialogType = "edit",
        this.dialogVisible = true;
      //发送请求获取当前节点最新数据显示在编辑对话框
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: 'get',
        params: this.$http.adornParams({})
      }).then(({ data }) => {
        //请求成功
        console.log("要回显的数据", data);
        this.category.name = data.data.name;
        this.category.catId = data.data.catId;
      })

    },

    append(data) {
      this.dialogType = "add",
        this.title = "添加分类",
        console.info("当前节点数据是", data);
      this.category.parentCid = data.catId
      this.category.catLevel = data.catLevel * 1 + 1
      this.dialogVisible = true;
      this.category.showStatus = 1;
    },

    remove(node, data) {
      console.info("当前节点", node.parent.data.catId);
      this.$confirm(`此操作将永久删除${data.name}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var ids = [data.catId];
          this.$http({
            url: this.$http.adornUrl("/product/category/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            console.info(data);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getMenus();
            this.expandedkeys = [node.parent.data.catId];
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    getMenus() {
      this.$http({
        url: this.$http.adornUrl("/product/category/list/tree"),
        method: "get",
      }).then(({ data }) => {
        console.log("获取菜单数据", data);
        this.menus = data.data;
      });
    },
  },
};
</script>